package com.thb.pay.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.sys.vo.req.SysRolePageReq;
import com.thb.pay.api.sys.vo.req.SysRoleReq;
import com.thb.pay.api.sys.vo.resp.SysRoleVo;
import com.thb.pay.api.sys.vo.resp.SysUserRoleVo;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.system.entity.SysRole;
import com.thb.pay.system.entity.SysRoleResource;
import com.thb.pay.system.entity.SysUserRole;
import com.thb.pay.system.mapper.SysRoleMapper;
import com.thb.pay.system.service.SysRoleResourceService;
import com.thb.pay.system.service.SysRoleService;
import com.thb.pay.system.service.SysUserRoleService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 系统-角色表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Slf4j
@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRoleResourceService sysRoleResourceService;

    @Override
    public PageResult<SysRoleVo> findPage(SysRolePageReq pageReq) {
        IPage<SysRole> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<SysRole> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, SysRoleVo.class);
    }

    @Override
    public SysRoleVo getById(Long id) {
        Assert.notNull(id, "roleId.not.null");
        SysRole sysRole = super.selectById(id);
        if (sysRole != null) {
            SysRoleVo sysRoleVo = new SysRoleVo();
            BeanUtil.copyProperties(sysRole, sysRoleVo);
            return sysRoleVo;
        }
        return null;
    }

    @Override
    public void add(SysRoleReq sysRoleReq) {
        Assert.notNull(sysRoleReq, "sysRoleReq.not.null");
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(sysRoleReq, sysRole);
        super.insert(sysRole);
    }

    @Override
    public void update(SysRoleReq sysRoleReq) {
        Assert.notNull(sysRoleReq, "sysRoleReq.not.null");
        Assert.notNull(sysRoleReq.getId(), "roleId.not.null");
        SysRole sysRole = super.selectById(sysRoleReq.getId());
        Assert.notNull(sysRole, "sysRole.not.exist");
        SysRole targetSysRole = new SysRole();
        BeanUtil.copyProperties(sysRoleReq, targetSysRole);
        super.updateById(targetSysRole);
    }

    @Override
    public void deleteById(Long id) {
        Assert.notNull(id, "roleId.not.null");
        SysRole sysRole = super.selectById(id);
        Assert.notNull(sysRole, "sysRole.not.exist");
        super.deleteById(id);

    }

    @Override
    public void deleteBatchIds(List<Long> ids) {
        Assert.notEmpty(ids, "roleIds.not.null");
        super.deleteBatchIds(ids);
    }

    @Override
    public List<SysRoleVo> findAllList() {
        List<SysRoleVo> sysRoleVoList = Lists.newArrayList();
        List<SysRole> sysRoleList = super.selectList(null);
        if (CollUtil.isNotEmpty(sysRoleList)) {
            sysRoleList.forEach(sysRole -> {
                SysRoleVo sysRoleVo = new SysRoleVo();
                BeanUtil.copyProperties(sysRole, sysRoleVo);
                sysRoleVoList.add(sysRoleVo);
            });
        }
        return sysRoleVoList;
    }

    @Override
    public List<SysUserRoleVo> findUserRoleListByUserId(Long userId) {
        List<SysUserRoleVo> targetResultList = Lists.newArrayList();
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> sysUserRoleList = sysUserRoleService.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(sysUserRoleList)) {
            sysUserRoleList.forEach(sysUserRole -> {
                SysUserRoleVo sysUserRoleVo = new SysUserRoleVo();
                BeanUtil.copyProperties(sysUserRole, sysUserRoleVo);
                targetResultList.add(sysUserRoleVo);
            });
        }
        return targetResultList;
    }

    @Override
    public void addResourceListByRoleId(Long roleId, List<Long> resourceIds) {
        Assert.notNull(roleId, "roleId.not.null");
        Assert.notEmpty(resourceIds, "resourceIds.not.null");
        LambdaQueryWrapper<SysRoleResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleResource::getRoleId, roleId);
        sysRoleResourceService.delete(wrapper);
        List<SysRoleResource> permissionList = Lists.newArrayList();
        resourceIds.forEach(resourceId -> {
            SysRoleResource sysRoleResource = new SysRoleResource();
            sysRoleResource.setResourceId(resourceId);
            sysRoleResource.setRoleId(roleId);
            permissionList.add(sysRoleResource);
        });
        sysRoleResourceService.insertBatch(permissionList, 15);
    }
}
